<script setup lang="ts">
import { User, Lock } from '@element-plus/icons-vue'
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import { useUserInfoStore } from '@/stores/userInfo';
import { useRouter } from 'vue-router';

const form = ref()
const userInfoStore = useUserInfoStore()
const router = useRouter()

// 登录表单数据
const loginForm = ref({
  account: '',
  password: ''
})

// 登录校验规则
const rules = {
  account: [
    { required: true, message: '请输入账号', trigger: 'blur' },
    { min: 5, max: 10, message: '账号必须是5-10位的字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { pattern: /^\S{6,15}$/, message: '密码必须是6-15位的非空字符', trigger: 'blur' }
  ]
}

const login = async () => {
  await form.value.validate()
  const success = await userInfoStore.login(loginForm.value)
  if (success) {
    ElMessage.success('登录成功')
    // 智能跳转到用户有权限的第一个页面
    const firstValidRoute = userInfoStore.visibleMenus.find(menu => menu.path || (menu.children && menu.children[0].path))
    if(firstValidRoute) {
      const path = firstValidRoute.path || firstValidRoute.children![0].path
      router.push(path!)
    } else {
      // 如果没有任何有效路由，可以跳转到一个默认的欢迎页或者提示页
      router.push('/user/info')
    }
  } else {
    ElMessage.error('登录失败，请检查账号或密码')
  }
}
</script>

<template>
  <div class="login-page">
    <div class="login-container">
      <div class="title-container">
        <div class="system-title">校园房源管理系统</div>
      </div>
      <el-form :model="loginForm" :rules="rules" ref="form" size="large" autocomplete="off" class="login-form">
        <el-form-item prop="account">
          <el-input v-model="loginForm.account" :prefix-icon="User" placeholder="请输入账号"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" :prefix-icon="Lock" type="password" placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="login" class="button" type="primary" auto-insert-space>
            登录
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<style lang="scss" scoped>
/* 样式 */
.login-page {
  height: 100vh;
  width: 100vw;
  display: flex;
  justify-content: center;
  align-items: center;
  background: url('../assets/login-background333.jpg') no-repeat center / cover;
  position: relative;
}

.login-container {
  width: 400px;
  padding: 40px;
  border-radius: 12px;
  background-color: rgba(255, 255, 255, 0.7);
  backdrop-filter: blur(10px);
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);

  .login-form {
    margin-top: 20px;

    .button {
      width: 100%;
    }
  }
}

.title-container {
  display: flex;
  justify-content: center;
  width: 100%;
  margin-bottom: 30px;
}

.system-title {
  font-size: 2.5rem;
  font-weight: bold;
  text-align: center;
  color: #409EFF;
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.2);
  font-family: "STXingkai", "华文行楷", serif;
  letter-spacing: 3px;
  background: linear-gradient(45deg, #42a5f5, #2196f3, #1976d2);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
  transform: perspective(400px) rotateX(10deg);
}
</style>
